package com.tengju.bff.application.wx;

import com.tengju.data.domain.model.wx.WxRequestRecord;
import com.tengju.data.domain.model.wx.WxRequestRecordRepository;
import com.tengju.data.domain.model.wx.WxRequestRecordStatus;
import com.tengju.user.domain.model.csm.dealer.CrmDealerInfo;
import com.tengju.user.domain.model.csm.dealer.CrmDealerInfoRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.tengju.data.domain.model.wx.WxRequestRecordType.*;

/**
 * @author: 李大仙
 * @date: 2021/11/5 11:04 上午
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class WxUserDepartmentRecordFailJobService {

    private final WxRequestRecordRepository wxRequestRecordRepository;
    private final CrmDealerInfoRepository crmDealerInfoRepository;
    private final WxUserDepartmentRecordService wxUserDepartmentRecordService;

    public void execute() {
        Map<Long, CrmDealerInfo> crmDealerInfoMap = getCrmDealerNameInfoMap();
        // 先执行删除
        processDelete();

        // 处理创建部门
        processCreateDept(crmDealerInfoMap);

        // 处理人员信息
        processCreateUser(crmDealerInfoMap);

        // 处理人员信息
        processUser();

        // 处理历史上级删除
        processHistoryDominationMove();

        // 处理部门leader
        processDepartmentLeader();

        // 处理历史上级新增
        processHistoryDominationAdd();
    }

    private void processHistoryDominationAdd() {
        List<WxRequestRecord> wxRequestRecordList = wxRequestRecordRepository.selectByStatus(
                List.of(HISTORY_ADD_INTO_CURRENT_DEPARTMENT),
                WxRequestRecordStatus.FAIL,
                "ASC");
        wxUserDepartmentRecordService.processHistoryDomination(wxRequestRecordList);
    }

    private void processHistoryDominationMove() {
        List<WxRequestRecord> wxRequestRecordList = wxRequestRecordRepository.selectByStatus(
                List.of(HISTORY_MOVE_OUT_CURRENT_DEPARTMENT),
                WxRequestRecordStatus.FAIL,
                "ASC");
        wxUserDepartmentRecordService.processHistoryDomination(wxRequestRecordList);
    }

    private void processCreateDept(Map<Long, CrmDealerInfo> crmDealerInfoMap) {
        List<WxRequestRecord> wxRequestRecordList = wxRequestRecordRepository.selectByStatus(
                List.of(CREATE_DEPARTMENT, CREATE_DEALER_DEPARTMENT,
                        CREATE_DEPARTMENT_LIMIT_ACCESS_TO_OUTSIDE_DEPARTMENTS_TAG),
                WxRequestRecordStatus.FAIL,
                "ASC");
        wxUserDepartmentRecordService.processCreateDept(crmDealerInfoMap, wxRequestRecordList);
    }

    private void processDepartmentLeader() {
        List<WxRequestRecord> wxRequestRecordList = wxRequestRecordRepository.selectByStatus(
                List.of(ADD_INTO_CURRENT_DEPARTMENT, MOVE_OUT_CURRENT_DEPARTMENT,
                        ADD_INTO_DEALER_DEPARTMENT, MOVE_OUT_DEALER_DEPARTMENT),
                WxRequestRecordStatus.FAIL,
                "ASC");
        wxUserDepartmentRecordService.processDepartmentLeader(wxRequestRecordList);
    }

    private void processCreateUser(Map<Long, CrmDealerInfo> crmDealerInfoMap) {
        List<WxRequestRecord> wxRequestRecordList = wxRequestRecordRepository.selectByStatus(
                List.of(CREATE_USER_DEPARTMENT),
                WxRequestRecordStatus.FAIL,
                "ASC");
        wxUserDepartmentRecordService.processCreateUser(crmDealerInfoMap, wxRequestRecordList);
    }

    private void processUser() {
        List<WxRequestRecord> wxRequestRecordList = wxRequestRecordRepository.selectByStatus(
                List.of(UPDATE_USER_DEPARTMENT, UPDATE_DEPARTMENT),
                WxRequestRecordStatus.FAIL,
                "ASC");
        wxUserDepartmentRecordService.processUser(wxRequestRecordList);
    }

    private void processDelete() {
        List<WxRequestRecord> wxRequestRecordList = wxRequestRecordRepository.selectByStatus(
                List.of(DELETE_DEPARTMENT, DELETE_USER_DEPARTMENT),
                WxRequestRecordStatus.FAIL,
                "DESC");
        wxUserDepartmentRecordService.processDelete(wxRequestRecordList);
    }

    public Map<Long, CrmDealerInfo> getCrmDealerNameInfoMap() {
        List<CrmDealerInfo> crmDealerInfoList = crmDealerInfoRepository.getCrmDealerNameInfo();
        return crmDealerInfoList.stream()
                .collect(Collectors.toMap(CrmDealerInfo::getIdCode, v -> v, (m1, m2) -> m1));
    }
}
